import requests,pytest
from untils.logger import logger
from conftest import login
from untils.data_loader import read_yaml
import logging
# 成功添加商品到购物车
@pytest.mark.parametrize('data1',read_yaml(r'C:\Users\张震海\Desktop\9_9\data\a3mall.yaml'))
def test_add1(login,data1):
    logger()
    logging.info('start')
    token = login
    url = 'http://10.59.9.12:7001/api/cart/add'
    header  = {"auth-token":"Bearer "+token}
    data = {
  "id": data1.get('id'),
  "sku_id": "",
  "num": 1
}
    add = requests.post(url=url,headers=header,data=data)
    print(add.json())
    # 断言状态码
    assert add.status_code == 200
    assert data1.get('exp') == add.json()['info']
    logging.info('last')



# token值为空添加商品添加失败 提示：“您还未登录，请登录”
def test_add2(login):
    logger()
    logging.info('start')
    token = login
    url = 'http://10.59.9.12:7001/api/cart/add'
    header  = {"auth-token":""}
    data = {
  "id": 108,
  "sku_id": "",
  "num": 1
}
    add = requests.post(url=url,headers=header,data=data)
    print(add.json())
    # 断言状态码
    assert add.status_code == 200
    assert add.json()['info'] == '您还未登录，请登录'
    logging.info('last')

# id值为空提示该商品已下架
def test_add3(login):
    logger()
    logging.info('start')
    logging.info('用例3添加')
    token = login
    url = 'http://10.59.9.12:7001/api/cart/add'
    header  = {"auth-token":"Bearer "+token}
    data = {
  "id": " ",
  "sku_id": "",
  "num": 1
}
    add = requests.post(url=url,headers=header,data=data)
    print(add.json())
    # 断言状态码
    assert add.status_code == 200
    assert add.json()['info'] == '该商品已下架！'
    logging.info('last')
#成功删除商品
def test_delete1(login):
    logger()
    logging.info('start')
    token = login
    url = 'http://10.59.9.12:7001/api/cart/delete'
    header  = {"auth-token":"Bearer "+token}
    data = {
  "id": "370"
}
    delete = requests.post(url=url,headers=header,json=data)
    print(delete.json())
    # 断言状态码
    assert delete.status_code == 200
    assert delete.json()['info'] == 'ok'
    logging.info('last')

# id 为空
def test_delete2(login):
    logger()
    logging.info('start')
    token = login
    url = 'http://10.59.9.12:7001/api/cart/delete'
    header  = {"auth-token":"Bearer "+token}
    data = {
  "id": " "
}
    delete = requests.post(url=url,headers=header,json=data)
    print(delete.json())
    # 断言状态码
    assert delete.status_code == 200
    logging.info('last')

# token值为空 提示：“您还未登录，请登录”
def test_delete3(login):
    logger()
    logging.info('start')
    token = login
    url = 'http://10.59.9.12:7001/api/cart/delete'
    header  = {"auth-token":""}
    data = {
  "id": "370"
}
    delete = requests.post(url=url,headers=header,json=data)
    print(delete.json())
    logging.info('last')
    # 断言状态码
    assert delete.status_code == 200
    assert delete.json()['info'] == '您还未登录，请登录'
# 成功修改商品数量
@pytest.mark.parametrize('data1',read_yaml(r'C:\Users\张震海\Desktop\9_9\data\a3mall2.yaml'))
def test_update1(login,data1):
    logger()
    logging.info('start')
    token = login
    url = 'http://10.59.9.12:7001/api/cart/change'
    data = {
  "id": data1.get('id'),
  "sku_id": 0,
  "num": data1.get('num')
}
    header = {"auth-token": "Bearer " + token}
    update = requests.post(url=url,headers=header,json=data)
    print(update.json())
    # 断言状态码
    assert update.status_code == 200

    assert data1.get('exp') == update.json()['info']
    logging.info('last')


# 修改id 为空提示 该商品已下架
def test_update2(login):
    logger()
    logging.info('start')
    token = login
    url = 'http://10.59.9.12:7001/api/cart/change'
    data = {
  "id": " ",
  "sku_id": 0,
  "num": "2"
}
    header = {"auth-token": "Bearer " + token}
    update = requests.post(url=url,headers=header,json=data)
    print(update.json())
    # 断言状态码
    assert update.status_code == 200
    assert update.json()['info'] =='该商品已下架！'
    logging.info('last')

# 成功查询
def test_search1(login):
    logger()
    logging.info('start')
    token = login
    header = {"auth-token": "Bearer " + token}
    url = 'http://10.59.9.12:7001/api/search/list?page=1&keywords=%E6%89%8B%E6%9C%BA&type=0&sort=1'
    data = {    "page":"1",
    "keywords":"手机",
    "type":'0',
    "sort":"1"}
    search = requests.get(url=url,headers=header,params=data)
    print(search.json())
    assert search.status_code == 200
    assert search.json()['info'] == "ok"
    logging.info('last')

# token值为空
def test_search2(login):
    logger()
    logging.info('start')
    token = login
    header = {"auth-token": ""}
    url = 'http://10.59.9.12:7001/api/search/list?page=1&keywords=%E6%89%8B%E6%9C%BA&type=0&sort=1'
    data = {    "page":"1",
    "keywords":"手机",
    "type":'0',
    "sort":"1"}
    search = requests.get(url=url,headers=header,params=data)
    print(search.json())
    assert search.status_code == 200
    assert search.json()['info'] == "ok"
    logging.info('last')